package physics;

import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.geometry.Text2D;

import java.awt.Color;
import java.awt.Font;
import java.awt.geom.Line2D;

import javax.media.j3d.*;
import javax.vecmath.Color3f;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;

public class Circle extends PhysicsObject {
	public float radius;
    public int number = -1;

	public Circle(float mass, float positionX, float positionY, float velocityX, float velocityY, float orientation, float angularVelocity, float radius, Color3f color1, Color3f color2) {
		super(mass, positionX, positionY, velocityX, velocityY, orientation, angularVelocity);
		
		if (radius <= 0)
			throw new IllegalArgumentException();
		
		momentOfInertia = mass * radius * radius / 2;
		centerOfMass.x = 0f;
		// Using the parallel axis theorem
		momentOfInertia += mass * centerOfMass.lengthSquared();
		this.radius = radius;
		TG.addChild(createShape(radius, 20, color1, color2));
	}

    public Circle(float mass, float positionX, float positionY, float velocityX, float velocityY,  float orientation, float angularVelocity, float radius, Color3f color1, Color3f color2, int number) {
        this(mass, positionX, positionY, velocityX, velocityY, orientation, angularVelocity, radius, color1, color2);
        this.number = number;
        TransformGroup textTG = new TransformGroup();
        Transform3D textT3D = new Transform3D();
        if (number > 9) {
            textT3D.setTranslation(new Vector3f(-.02f, -.02f, radius));
        } else {
            textT3D.setTranslation(new Vector3f(-.01f, -.02f, radius));
        }
        textTG.setTransform(textT3D);
        Text2D text2d = new Text2D(String.valueOf(number), new Color3f(0,0,0), "Arial", 9, Font.BOLD);
        textTG.addChild(text2d);
        TG.addChild(textTG);
    }
	
	private Node createShape(float radius, int samples, Color3f color1, Color3f color2) {
		Appearance appearance = new Appearance();
        if (color1 == null) {
            color1 = new Color3f(Color.getHSBColor((float)Math.random(), 1, 1));
        }
        Material mat = new Material();
        mat.setAmbientColor(color1);
        mat.setEmissiveColor(color1);
        mat.setDiffuseColor(color1);
        mat.setSpecularColor(new Color3f(1f,1f,1f));
        appearance.setMaterial(mat);
		appearance.setColoringAttributes(new ColoringAttributes(color1, ColoringAttributes.FASTEST));
        Node root = new Sphere(radius, appearance);
		return root;
	}
}
